<?php

include 'confidential_settings.php';
// This include contains 5 variables:
//  - db_name: database name
//  - table_name_x: table name
//  - username: username
//  - password: password
//  - localhost: host name

// Generate 4 files:
// * base_vars.js with content:
//	WRInfo[0]=new Array("SMA SB 2100","SMA SB 2100",4711,0,"SMA Sunny Boy 2100TL",1,null,null,0,null,1,0,0,1000,null)
// * days_hist.js with content:
//	da[dx++]="14.02.09|1921;2100"
//	da[dx++]="13.02.09|10292;2100"
//	da[dx++]="12.02.09|321;2100"
//	da[dx++]="11.02.09|1112;2100"
// * months.js with content:
//	mo[mx++]="14.02.09|123456"
//	mo[mx++]="31.01.09|123456"
//	mo[mx++]="31.12.08|123456"
// * min_day.js as empty file
//


function get_corr_value_day($year,$month,$day){
	include 'gsc_corrections.php';

	$ret_corr_value = 0;
	for($i=0; $i< count($corr_year); $i++){
		if((trim($corr_year[$i]) == trim($year))&&(trim($corr_month[$i]) == trim($month))&&(trim($corr_day[$i]) == trim($day))){
			$ret_corr_value = $corr_value[$i];
			return $ret_corr_value;
		}
	}
	return $ret_corr_value;
}

function get_corr_value_month($year,$month){
	include 'gsc_corrections.php';

	$ret_corr_value = 0;
	for($i=0;$i<count($corr_year);$i++){
		if((trim($corr_year[$i]) == trim($year))&&(trim($corr_month[$i]) == trim($month))){
			$ret_corr_value = $ret_corr_value + $corr_value[$i];
		}
	}
	return $ret_corr_value;
}


echo "GENERATING PV LOG FILES:<br/><ul>";

$dbh=mysql_connect ($localhost, $username, $password) or die('Cannot connect to the database because: ' . mysql_error());

mysql_select_db ($db_name) or die(mysql_error()); 

// Note: originally the production of the current day was not added to the statistics, this has been removed now!
//       You could always re-introduce this by adding the clause 'WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) >= DATE(date_entered)' after the 'FROM' clause.
$result_day   = mysql_query("SELECT YEAR(date_entered), DATE_FORMAT(date_entered,'%y') as year_string, MONTH(date_entered), DATE_FORMAT(date_entered,'%m') as month_string, DAY(date_entered), DATE_FORMAT(date_entered,'%d') as day_string, SUM(val) FROM $table_name_gsc GROUP BY YEAR(date_entered), MONTH(date_entered), DAY(date_entered) ORDER BY DATE(date_entered) DESC");
$result_month = mysql_query("SELECT YEAR(date_entered), DATE_FORMAT(date_entered,'%y') as year_string, MONTH(date_entered), DATE_FORMAT(date_entered,'%m') as month_string, SUM(val) FROM $table_name_gsc  GROUP BY YEAR(date_entered), MONTH(date_entered) ORDER BY DATE(date_entered) DESC");

/*
echo "<br /><br />DAY VALUES:<br />";
while($row = mysql_fetch_array($result)){
	echo $row['DATE(date_entered)'] . "\t" . $row['SUM(val)'];
	echo "<br />";
}
*/

mysql_close($dbh);

// DAYS_HIST.JS
echo "<li>Dumping file days_hist.js ...";
$fn_days_hist = "../pv_log_files/days_hist.js";
$handle_days_hist = fopen($fn_days_hist, 'w') or die("can't open file");
$last_day = 0;
$last_month = 0;
$last_year = 0;
while($row = mysql_fetch_array($result_day)){
	if($last_day == 0){
		$last_day = $row['day_string'];
		$last_month = $row['month_string'];
		$last_year = $row['year_string'];
	}
	$tmp_value = $row['SUM(val)'] + (1000*get_corr_value_day($row['YEAR(date_entered)'],$row['MONTH(date_entered)'],$row['DAY(date_entered)']));
	$stringData = "da[dx++]=\"".$row['day_string'].".".$row['month_string'].".".$row['year_string']."|".$tmp_value.";2100\"\n";
	fwrite($handle_days_hist, $stringData);
}
fclose($handle_days_hist);
echo " OK<br/>";


// MONTHS.JS
$cur_day = 1;
echo "<li>Dumping file months.js ...";
$fn_months = "../pv_log_files/months.js";
$handle_months = fopen($fn_months, 'w') or die("can't open file");
while($row = mysql_fetch_array($result_month)){
	if($cur_day == 0){
	$nb_days = 0;
	switch($row['MONTH(date_entered)']){
	case 1:  $nb_days = 31; break;
	case 2:  
		$year = $row['YEAR(date_entered)'];
		if( $year % 4 == 0 && $year % 400 != 0 ){
			$nb_days = 29; // extra day
		}else{
			$nb_days = 28;
		} break;
	case 3:  $nb_days = 31; break;
	case 4:  $nb_days = 30; break;
	case 5:  $nb_days = 31; break;
	case 6:  $nb_days = 30; break;
	case 7:  $nb_days = 31; break;
	case 8:  $nb_days = 31; break;
	case 9:  $nb_days = 30; break;
	case 10: $nb_days = 31; break;
	case 11: $nb_days = 30; break;
	case 12: $nb_days = 31; break;
	}
	}else{
	$nb_days = $last_day;
	$cur_day = 0;
	}
	$tmp_value = $row['SUM(val)'] + (1000*get_corr_value_month($row['YEAR(date_entered)'],$row['MONTH(date_entered)']));
	$stringData = "mo[mx++]=\"".$nb_days.".".$row['month_string'].".".$row['year_string']."|".$tmp_value."\"\n";
	fwrite($handle_months, $stringData);
}
fclose($handle_months);
echo " OK<br/>";

// MIN_DAY.JS
echo "<li>Dumping file min_day.js ...";
$fn_min_day = "../pv_log_files/min_day.js";
$handle_min_day = fopen($fn_min_day, 'w') or die("can't open file");
$stringData = "\n";
fwrite($handle_min_day, $stringData);
fclose($handle_min_day);
echo " OK<br/>";

// BASE_VARS.JS
echo "<li>Dumping file base_vars.js ...";
$fn_base_vars = "../pv_log_files/base_vars.js";
$handle_base_vars = fopen($fn_base_vars, 'w') or die("can't open file");
$stringData = "WRInfo[0]=new Array(\"SMA SB 2100\",\"SMA SB 2100\",4711,0,\"SMA Sunny Boy 2100TL\",1,null,null,0,null,1,0,0,1000,null)\n";
fwrite($handle_base_vars, $stringData);
fclose($handle_base_vars);
echo " OK<br/>";

echo "</ul><br/>";

?>
